package com.itheima;

import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class StreamDemo {
    public static void main(String[] args) {
        List<String> names = new ArrayList<String>();
        names.add("zhangsan");
        names.add("lisi");
        names.add("wangwu");
        names.add("zhaoliu");
        names.add("qianqi");
        names.add("sunba");

        List<String> list1 = new ArrayList<>();
        //遍历names集合
        names.forEach(name -> {
            //以z开头的添加到list1集合中
            if (name.startsWith("z")) {
                list1.add(name);
            }
        });

        List<String> list2 = new ArrayList<>();
        list1.forEach(name -> {
            if (name.startsWith("w")) {
                list2.add(name);
            }
        });

        //1.获取集合的流对象Stream对象
        //2.筛选，获取符合条件的元素
        //3.终结方法，遍历集合
        names.stream()
                .filter(name -> name.startsWith("z"))
                .filter(name -> name.length() == 3)
                .forEach(System.out::println);

    }

    @Test
    public void test1() {
        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(5);
        list.add(6);
        list.add(7);
        list.add(8);
        list.add(9);
        list.add(10);

        //最后调用收集方法
        //List<Integer>list1 =list.stream().filter(integer -> integer % 2 == 0).collect(Collectors.toList());
        List<Integer>list1 =list.stream().filter(integer -> integer % 2 == 0).toList();
        System.out.println(list1);

    }

    @Test
    public void test2(){
        List<String> list = new ArrayList<>();
        list.add("zhangsan.20");
        list.add("lisi.21");
        list.add("wangwu.22");
        //过滤年龄大于20的
        Map<Object,Object> map = list.stream().filter(s -> {
            String[] split = s.split("\\.");
            int age = Integer.parseInt(split[1]);
            return age > 20;
        }).collect(Collectors.toMap(s -> s.split("\\.")[0], s -> s.split("\\.")[1]));
        map.forEach((k,v)->{
            System.out.println(k+"---"+v);
        });

    }

    //流的合并
    @Test
    public void test3(){
        List<String> list1 = new ArrayList<>();
        //男演员
        list1.add("成龙");
        list1.add("邓超");
        list1.add("张学友");
        list1.add("王宝强");

        List<String> list2 = new ArrayList<>();
        //女演员
        list2.add("刘亦菲");
        list2.add("林心如");
        list2.add("王祖贤");
        list2.add("蔡依林");

        Stream<String> stream1 =list1.stream().filter(new Predicate<String>() {
            @Override
            public boolean test(String s) {
                return s.length()==3;
            }
        }).limit(2);

        Stream<String> stream2 = list2.stream().filter(s -> s.startsWith("林")).skip(1);

        //合并流
        Stream.concat(stream1,stream2).forEach(System.out::println);

    }
}
